/*
 * Copyright 2010 Michael Nitsche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.mnit.basis.daten;

import java.lang.reflect.Array;


/**
 * @author Michael Nitsche
 */
public class ArrayHilfe {

	@SuppressWarnings("unchecked")
	public static <T> T[] nArray(Class<T> klasse, int laenge) {
		return (T[])Array.newInstance(klasse,laenge);
	}

	@SuppressWarnings("unchecked")
	public static <T> T[][] nArray2(Class<T> klasse, int laenge1, int laenge2) {
		return (T[][])Array.newInstance(klasse, new int[]{laenge1, laenge2});
	}

	public static <T> T[] verbinden(Class<T> klasse, T[] o1, T o2) {
		T[] erg=ArrayHilfe.nArray(klasse, o1.length+1);
		System.arraycopy(o1, 0, erg, 0, o1.length);
		erg[erg.length-1]=o2;
		return erg;
	}

	public static <T> T[] verbinden(Class<T> klasse, T o1, T[] o2) {
		T[] erg=ArrayHilfe.nArray(klasse, o2.length+1);
		System.arraycopy(o2, 0, erg, 1, o2.length);
		erg[0]=o1;
		return erg;
	}

	/**
	 * @deprecated Muß noch ausführlich getestet werden!!!
	 */
	public static <T> T[] verbinden(Class<T> klasse, T[] o1, T[] o2) {
		T[] erg=ArrayHilfe.nArray(klasse, o1.length+o2.length);
		System.arraycopy(o1, 0, erg, 0, o1.length);
		System.arraycopy(o2, 0, erg, o1.length, o2.length);
		return erg;
	}

	public static <T> T[] schneiden(Class<T> klasse, T[] quell, int von, int laenge) {
		T[] erg=ArrayHilfe.nArray(klasse, laenge);
		System.arraycopy(quell, von-1, erg, 0, laenge);
		return erg;
	}

	public static <T> boolean kennt(T[] bereich, T kennt) {
		for(T o : bereich)
			if(o.equals(kennt))
				return true;
		return false;
	}

}
